<?php
/**
 * Users management
 */
class User{
	/**
	 * database connection
	 * @var Object
	 */
	private $MySQL;

	/**
	 * Id
	 * @var integer
	 */
	private $id;

	/**
	 * Email
	 * @var string
	 */
	private $email;

	/**
	 * Password
	 * @var string
	 */
	private $password;

	/**
	 * Tasks list of this user
	 * @var <array> of arrays, each one indexed with tasksList fields (id, name, fk_user)
	 */
	private $lists;

	/**
	 * Construct
	 *
	 * Load the user from the database throw its id {@link load()}. If the id is null create an empty Object
	 * @see load()
	 *
	 * @param integer $id
	 */
	public function __construct($id=null){
		$this->MySQL = MySQL::get_instance();

		if($id){
			$this->id = $id;
			$this->load();
		}
	}

	/**
	 * Load the user data from de DB
	 */
	private function load(){
		try{
			if($this->id){
				$query = "SELECT *
							FROM users
							WHERE id = '$this->id'; ";
				
				if(!($result = $this->MySQL->query($query))){
					throw new Exception("Cannot load the user");
				}
				else if(mysql_num_rows($result) == 0){
					throw new Exception("There is no user");
				}

				$row = mysql_fetch_array($result);

				$this->password = $row['password'];
				$this->email = $row['email'];

				$this->load_lists();
			}
		}catch(Exception $e){}
	}

	/**
	 * Load the tasks lists of the user
	 */
	private function load_lists(){
		$query = "SELECT * FROM tasks_lists WHERE fk_user='$this->id';";

		if(!($result = $this->MySQL->query($query))){
			throw new Exception("Cannot load the lists of the user");
		}

		while($row=  mysql_fetch_array($result)){
			$this->lists[$row['id']] = $row;
		}
	}

	/**
	 * Get the id
	 *
	 * @return integer $id 
	 */
	public function get_id(){
		return $this->id;
	}

	/**
	 * Get the password
	 *
	 * @return string $password
	 */
	public function get_password(){
		return $this->password;
	}

	/**
	 * Get the email
	 * @return <string>
	 */
	public function get_email(){
		return $this->email;
	}

	/**
	 * Get the tasks lists
	 * @return <array> of arrays, each one indexed with de list fields (id, name, fk_user)
	 */
	public function get_lists(){
		return $this->lists;
	}

        
	/**
	 * Change password
	 * @param <string> $pass
	 */
	public function set_password($pass){
		if($pass != ''){
			$pass = md5($pass);
			$query = "UPDATE users SET password='$pass' WHERE id='$this->id';";
			if(!$this->MySQL->query($query))
				throw new Exception("Error where editing the user password");
			$this->password = $pass;
		}
	}

	/**
	 * Change email
	 * @param <string> $email
	 */
	public function set_email($email){
		if($email != '' && $email != $this->email && Validate::email($email)){
			$email = $email;
			$query = "UPDATE users SET email='$email' WHERE id='$this->id';";
			if(!$this->MySQL->query($query))
				throw new Exception("Error where editing the user email");
			$this->email = $email;
		}
	}
	
	/**
	 * Create a new User
	 * @param <array> $data
	 * @return <integer> $id new id in the DB
	 */
	public function create($data){

		$errors = '';
		if($data['password'] == '' || ! isset($data['password']))
			$errors .= "Error in password";
		if($data['email'] == '' || ! isset($data['email']) || !Validate::email($data['email']))
			$errors .= "Error in email";

		if($errors != '') {
			throw new Exception($errors);
		}

		$this->password = md5(trim($data['password']));
		$this->email = stripslashes(trim(mysql_real_escape_string($data['email'])));

		$this->save();

		return $this->id;
	}

	/**
	 * Saving the user in the DB
	 */
	private function save(){
		$query = "INSERT INTO users (email, password)
							VALUES (
									'$this->email',
									'$this->password'
									);";
		if(!$this->MySQL->query($query))
			throw new Exception("Error creating the user");

		$this->id = mysql_insert_id();
	}

	/**
	 * Creates a new lists for the user
	 * @param <string> $name
	 */
	public function add_list($name){
		$tasks_list = new TasksList();
		$data = array('name' => $name, 'user_id' => $this->id);

		$tasks_list->create($data);
		$this->load_lists();

		return $tasks_list;
	}
}
?>